<?php
namespace app\account\controller;

use app\account\model\GradeManager;

class Index
{
    public function index()
    {
        //echo $this->isNull($_GET["tel"],$_GET["passd"]);
        $gradeManager = new GradeManager();
        echoJson( 1, $gradeManager->isGrade($_GET["uid"]));

//        return '<style type="text/css">*{ padding: 0; margin: 0; } .think_default_text{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ad_bd568ce7058a1091"></think>';
    }

    /**
     *  判断账号或密码是否为空
     * @param str $tel 账号
     * @param str $passd 密码
     * @return boolean
     */
    /*public function isNull($tel, $authCode, $passd) {
        if (empty($tel) || mb_strlen($tel, "UTF-8") != 11) {
            echo echoJson(0, "电话号为空或不合法!");
            return false;
        }
        if (empty($authCode) || mb_strlen($authCode, "UTF-8") != 6) {
            echo echoJson(0, "验证码为空或不合法!");
            return false;
        }
        if (empty($passd) || mb_strlen($passd, "UTF-8") < 8 || mb_strlen($passd, "UTF-8") > 16) {
            echo echoJson(0, "密码为空或不合法!!");
            return false;
        }
        return true;
    }*/

    /**
     * 电话号是否存在
     * @param str $tel 电话
     * @param str $passd 密码
     */
    public function isTelExists($tel, $authCode, $passd) {
        $sql = "SELECT tel FROM user where tel='$tel'";
        if (selectSQLRow($sql)) {//判断账号是否已存在
            SMS_verification($tel, $authCode, $passd);
        } else {
            echo echoJson(0, "电话不存在，请重新输入!");
        }
    }

    /**
     * 匹配短信验证码
     * @param str $tel 电话
     * @param str $authCode 验证码
     * @param str $passd 密码
     */
    public function SMS_verification($tel, $authCode, $passd) {
        $sql = "SELECT find_password_code,find_password_time FROM SMS_verification where tel='$tel'";
        $row = selectSQLRow($sql);
        if ($row['find_password_code'] == $authCode && time() <= ($row['find_password_time'] + 1000 * 10)) {//判断验证码是否匹配
            fandPssd($tel, $passd);
        } else {
            echo echoJson(0, "验证码错误或已过期!");
        }
    }

    /**
     * 修改密码
     * @param str $tel 账号
     * @param str $passd 密码
     */
    public function fandPssd($tel, $passd) {
        $sql = "INSERT INTO user (tel,passd) VALUES ('$tel','$passd')  ON DUPLICATE KEY UPDATE tel='$tel',passd='$passd'";
        if (insertSQL($sql)) {
            echo echoJson(1, "密码修改成功!");
        } else {
            echo echoJson(0, "密码修改失败，请稍后再试!");
        }
    }
}
